/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateNewAccPanel.java
 *
 * Created on 2011-11-3, 15:32:09
 */
package view.frontoperator;

import business.CreateNewAccCmd;
import java.math.BigDecimal;
import java.util.Date;
import javax.swing.JOptionPane;
import model.Account;
import model.AccountFactory;

/**
 *
 * @author Administrator
 */
public class CreateNewAccPanel extends javax.swing.JPanel {

    /** Creates new form CreateNewAccPanel */
    public CreateNewAccPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        type = new javax.swing.ButtonGroup();
        cla = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        owerIDField = new javax.swing.JTextField();
        balanceField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        fixedBtn = new javax.swing.JRadioButton();
        currentBtn = new javax.swing.JRadioButton();
        confirmBtn = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        individualBtn = new javax.swing.JRadioButton();
        vipBtn = new javax.swing.JRadioButton();
        enterpriseBtn = new javax.swing.JRadioButton();

        jLabel1.setText("身份证号");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("开户类型");
        jLabel2.setName("jLabel2"); // NOI18N

        owerIDField.setName("owerIDField"); // NOI18N

        balanceField.setName("balanceField"); // NOI18N

        jLabel3.setText("初始金额");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("设定密码");
        jLabel4.setName("jLabel4"); // NOI18N

        passwordField.setName("passwordField"); // NOI18N

        type.add(fixedBtn);
        fixedBtn.setSelected(true);
        fixedBtn.setText("定期");
        fixedBtn.setName("fixedBtn"); // NOI18N

        type.add(currentBtn);
        currentBtn.setText("活期");
        currentBtn.setName("currentBtn"); // NOI18N

        confirmBtn.setText("确定");
        confirmBtn.setName("confirmBtn"); // NOI18N
        confirmBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmBtnActionPerformed(evt);
            }
        });

        jLabel5.setText("账户类型");
        jLabel5.setName("jLabel5"); // NOI18N

        individualBtn.setSelected(true);
        individualBtn.setText("普通个人用户");
        individualBtn.setName("individualBtn"); // NOI18N

        vipBtn.setText("VIP个人用户");
        vipBtn.setName("vipBtn"); // NOI18N

        enterpriseBtn.setText("企业用户");
        enterpriseBtn.setName("enterpriseBtn"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(confirmBtn))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(125, 125, 125)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(individualBtn)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                                    .addComponent(owerIDField, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                                    .addComponent(balanceField, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                                    .addComponent(vipBtn)
                                    .addComponent(enterpriseBtn))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(fixedBtn)
                                    .addGap(18, 18, 18)
                                    .addComponent(currentBtn)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 125, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addGap(134, 134, 134))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(115, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(owerIDField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(fixedBtn)
                    .addComponent(currentBtn))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(balanceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(individualBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vipBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(enterpriseBtn)
                .addGap(12, 12, 12)
                .addComponent(confirmBtn)
                .addGap(68, 68, 68))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void confirmBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmBtnActionPerformed
        // TODO add your handling code here:
        AccountFactory af = new AccountFactory();
        Account account = null;
        if(individualBtn.isSelected())
            account = af.generateAccount(AccountFactory.AccountClass.INDIVIDUAL);
        else if(vipBtn.isSelected())
            account = af.generateAccount(AccountFactory.AccountClass.VIP);
        else if(enterpriseBtn.isSelected())
            account = af.generateAccount(AccountFactory.AccountClass.ENTERPRISE);
        CreateNewAccCmd cmd = new CreateNewAccCmd();
        cmd.setAccount(account);
        account.setBalance(new BigDecimal(balanceField.getText()));
        account.setCreateDate(new Date());
        account.setOwnerID(owerIDField.getText());
        account.setPwd(String.valueOf(passwordField.getPassword()));
        account.setType(fixedBtn.isSelected()? Account.AccountType.FIXED:Account.AccountType.CURRENT);
        if( cmd.execute(null) ) {
            JOptionPane.showMessageDialog(this, "创建新账户成功,账户号为 " + account.getId() + " ", "成功", JOptionPane.INFORMATION_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(this, "创建新账户失败 ", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_confirmBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField balanceField;
    private javax.swing.ButtonGroup cla;
    private javax.swing.JButton confirmBtn;
    private javax.swing.JRadioButton currentBtn;
    private javax.swing.JRadioButton enterpriseBtn;
    private javax.swing.JRadioButton fixedBtn;
    private javax.swing.JRadioButton individualBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField owerIDField;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.ButtonGroup type;
    private javax.swing.JRadioButton vipBtn;
    // End of variables declaration//GEN-END:variables
}
